package com.running.mms.smil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class SmilFileReader {
	
	private Element elt;
	private String basePath = "";
	
	public SmilFileReader(String path){
		SAXBuilder sb = new SAXBuilder();  
        try {
        	path = path.replace("\\", "/");
//        	假设smil文件和本条彩信所需的要内容（txt,img）在同一个目录下。
        	basePath = path.substring(0, path.lastIndexOf("/")+1);
            Document doc = sb.build(new File(path));
            this.elt = doc.getRootElement();  
        } catch (JDOMException e) {
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
	}
	
	public List <Map> getPars(){
		List <Map> pars = new ArrayList<Map>();
		try{			
			List<Element> elements = (List<Element>)XPath.selectNodes(elt, "//smil/body/par");
			for(Element _element:elements){
				Map par = new HashMap();
				Element radio = _element.getChild("radio");
				Element text = _element.getChild("text");
				Element img = _element.getChild("img");
				if(null != radio){
					par.put("voiFileName", radio.getAttributeValue("src"));
				}
				
				if(null != text){
					par.put("txtFileName", text.getAttributeValue("src"));
				}
				
				if(null != img){
					par.put("imgFileName", img.getAttributeValue("src"));
				}
				
				if (null != _element.getAttribute("dur")) {
					par.put("frameTime", _element.getAttributeValue("dur").replace("000ms", ""));
				}
				pars.add(par);
			}
		}catch(JDOMException e){
			e.printStackTrace();
		}
		return pars;
	}
}
